// pages/myself/info/info.js
var Zan = require('../../../dist/index.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
Page(Object.assign({}, Zan.TopTips, Zan.Toast, {

  /**
   * 页面的初始数据
   */
  data: {
    mobile: "",
    // sex: ['男', '女'], // 男=1， 女=2
    // sexIndex: 0,
    date: '',
    avatarUrl: null,
    nickName: null,
    gender: '未知',
    formId: '',
    name:'',
    canIUseMobile: wx.canIUse('button.open-type.getPhoneNumber')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    console.log("======================");
    console.log(app.globalData.userInfo);
    this.setData({
      avatarUrl: app.globalData.userInfo.avatarUrl,
      nickName: app.globalData.userInfo.nickName,
      sex: app.globalData.userInfo.gender,
      name: app.globalData.userInfo.name,
      mobile: app.globalData.userInfo.mobile,
    })
    if (app.globalData.userInfo.dateOfBirth){
      if (app.globalData.userInfo.dateOfBirth.length > 10){
        this.setData({
          date: app.globalData.userInfo.dateOfBirth.substring(0,10)
        })
      }else{
        this.setData({
          date: app.globalData.userInfo.dateOfBirth
        })
      }
    }
    if (app.globalData.userInfo.gender == 0) {
      this.setData({
        gender: '未知'
      })
    } else if (app.globalData.userInfo.gender == 1) {
      this.setData({
        gender: '男'
      })
    } else if (app.globalData.userInfo.gender == 2) {
      this.setData({
        gender: '女'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  /**
   * 选择性别
   */
  // bindPickerChange: function (e) {
  //   this.setData({
  //     sexIndex: e.detail.value
  //   })
  // },
  /**
   * 日期
   */
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },

  /**
   * 修改资料
   */
  formSubmit: function (e) {
    let name = e.detail.value.name;
    //var sex = parseInt(this.data.sexIndex) + 1;
    this.setData({
      name: name,
    })

    network._post_json(this, api.modifyUserInfo, { nickName: this.data.nickName, avatarUrl: this.data.avatarUrl, name: name, gender: this.data.sex, dateOfBirth: this.data.date }, success);
  },

  // formIdSubmit: function (e) {
  //   this.setData({
  //     formId: e.detail.formId
  //   })
  // },

  getPhoneNumber: function (e) {
    let that = this;
    if (e.detail.encryptedData) {
      // 校验用户当前session_key是否有效
      wx.checkSession({
        success: function () {
          network._post_json(that, api.decodePhone, { appId: app.globalData.appId, encryptedData: e.detail.encryptedData, iv: e.detail.iv, sessionKey: app.globalData.sessionKey }, bindSuccess);
        },
        fail: function () {
          // session_key 已经失效，重新登录
          wx.login({
            success: function (res) {
              // 获取jsCode
              let jsCode = res.code;
              wx.request({
                url: api.getOpenId,
                method: 'POST',
                data: { prAppid: app.globalData.appId, code: jsCode },
                success: function (res) {
                  console.log(res.data);
                  let sessionKey = res.data.data.session_key;
                  network._post_json(that, api.decodePhone, { appId: app.globalData.appId, encryptedData: e.detail.encryptedData, iv: e.detail.iv, sessionKey: sessionKey }, bindSuccess);
                }
              })
            },
            fail: function (res) {
              console.log(res);
            }
          })

        }
      })

    }
  },

  // refresh: function (mobile) {
  //   this.setData({
  //     mobile: mobile
  //   })
  // }

}))

var success = function (that, res) {
  that.showZanToast('资料修改成功');
  if (res.data.data.id) {
    app.globalData.userInfo.name = res.data.data.name;
  }
  app.globalData.userInfo.gender = res.data.data.gender;
  app.globalData.userInfo.dateOfBirth = res.data.data.dateOfBirth;
}

var bindSuccess = function (that, res) {
  if (res.data.data) {
    app.globalData.userInfo.mobile = res.data.data.mobile;
    that.setData({
      userInfo: app.globalData.userInfo,
      mobile: res.data.data.mobile
    });
  }
}